package team.seekers.oj.third.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.exception.ApiException;
import team.seekers.oj.third.service.VodService;
import team.seekers.oj.third.utils.ConstantVodUtils;
import team.seekers.oj.third.utils.InitObject;

import java.io.InputStream;
import java.util.List;

/**
 * @author Godc
 * @description
 */
@Service
public class VodServiceImpl implements VodService {


    @Override
    public void removeVideo(String id){
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            //创建一个删除视频的对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //设置视频id
            request.setVideoIds(id);
            client.getAcsResponse(request);
        }catch (Exception e){
            throw new ApiException(ApiExceptionEnum.INTERNAL_ERROR,"删除视频失败");
        }
    }

    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));

            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantVodUtils.KEY_ID,
                    ConstantVodUtils.KEY_SECRET,
                    title, originalFilename, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = null;
            if (!response.isSuccess()) {
                videoId = response.getVideoId();
            }else{
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void removeBatchAlyVideo(List videoList) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            //创建一个删除视频的对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            String join = StringUtils.join(videoList, ",");
            //设置视频id值
            request.setVideoIds(join);
            client.getAcsResponse(request);
        }catch (Exception e){
            throw new ApiException(ApiExceptionEnum.INTERNAL_ERROR,"删除视频失败");
        }
    }
}
